package com.lzx.iteam.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lzx.iteam.R;
import com.lzx.iteam.ScheduleCalendarActivity;
import com.lzx.iteam.bean.EventListData;
import com.lzx.iteam.bean.ScheduleData;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 03月 15日
 * Time: 下午5:43
 *
 * @QQ : 1234567890
 * **********************************
 */
public class CalendarListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Object> mListData = new ArrayList<Object>();
    private boolean isMy;         // true：是我本人      false：不是本人
    private boolean mRedLineShow; // true: 今天显示红线   false：不显示红线
    private long startTime;

    public CalendarListAdapter(Context mContext, boolean isMy) {
        this.mContext = mContext;
        this.isMy = isMy;
    }

    public void bindData(List<Object> list, boolean isShow) {
        this.mListData = list;
        this.mRedLineShow = isShow;
        notifyDataSetChanged();
    }

    /**
     * 当在当天删除日程时
     */
    public void setShowLine() {
        this.mRedLineShow = true;
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.calendar_item_list_layout, null);
            holder = new ViewHolder();
            holder.dayTime = (ImageView) convertView.findViewById(R.id.calendar_list_iv_day);
            holder.status = (ImageView) convertView.findViewById(R.id.calendar_list_iv_status);
            holder.type = (ImageView) convertView.findViewById(R.id.calendar_list_iv_type);
            holder.time = (TextView) convertView.findViewById(R.id.calendar_list_tv_time);
            holder.done = (ImageView) convertView.findViewById(R.id.calendar_list_iv_check);
            holder.doneLayout = (LinearLayout) convertView.findViewById(R.id.calendar_list_ll_check);
            holder.title = (TextView) convertView.findViewById(R.id.calendar_list_tv_title);
            holder.mRedLine = (RelativeLayout) convertView.findViewById(R.id.calendar_red_line);
            holder.currentTime = (TextView) convertView.findViewById(R.id.calendar_current_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (mListData != null && mListData.size() > 0) {

            if (mListData.get(position) instanceof ScheduleData) {

                final ScheduleData data = (ScheduleData) mListData.get(position);
                startTime = Long.parseLong(data.getStartTime()) * 1000;
                holder.time.setText(DateUtil.getHourAndMin(startTime));
                int hour = DateUtil.getHour(startTime);
                if (hour <= 12) {
                    holder.dayTime.setBackgroundResource(R.drawable.calendar_morning_iv);
                } else if (hour >= 18) {
                    holder.dayTime.setBackgroundResource(R.drawable.calendar_evening_iv);
                } else {
                    holder.dayTime.setBackgroundResource(R.drawable.calendar_afternoon_iv);
                }
                if (!StringUtil.isEmpty(data.getEndTime())) {
                    Long endTime = Long.parseLong(data.getEndTime()) * 1000;
                    if (endTime != 0) {
                        holder.time.append("-" + DateUtil.getHourAndMin(endTime));
                    }
                }
                if (isMy || "0".equals(data.getHidden())) {
                    if (!StringUtil.isEmpty(data.getAddress())) {
                        holder.time.append("     地点：" + data.getAddress());
                    }
                    holder.title.setText(data.getTitle());
                    if ("1".equals(data.getHasRead())) {
                        holder.status.setVisibility(View.GONE);
                    } else {
                        holder.status.setVisibility(View.VISIBLE);
                    }

                    if (data.getPublishedUId() != null) {
                        if (!data.getPublishedUId().equals("0")) {
                            if ("1".equals(data.getDone())) {
                                holder.title.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                                holder.time.setTextColor(mContext.getResources().getColor(R.color.schedule_bule_text2));
                                holder.title.setTextColor(mContext.getResources().getColor(R.color.schedule_bule_text2));
                            } else {
                                holder.title.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                                holder.time.setTextColor(mContext.getResources().getColor(R.color.schedule_bule_text1));
                                holder.title.setTextColor(mContext.getResources().getColor(R.color.schedule_bule_text1));
                            }
                        }else{
                            if ("1".equals(data.getDone())) {
                                holder.title.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                                holder.time.setTextColor(mContext.getResources().getColor(R.color.schedule_create_time));
                                holder.title.setTextColor(mContext.getResources().getColor(R.color.schedule_manager_no_data));
                            } else {
                                holder.title.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                                holder.time.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
                                holder.title.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
                            }
                        }
                    }

                } else {
                    holder.title.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                    holder.time.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
                    holder.title.setTextColor(mContext.getResources().getColor(R.color.schedule_content));
                    holder.time.append("     地点：已隐藏");
                    holder.title.setText("日程内容已隐藏");
                }
                holder.type.setVisibility(View.GONE);
                if (isMy) {
                    holder.done.setVisibility(View.VISIBLE);
                    String done = data.getDone();
                    if ("1".equals(done)) {
                        holder.done.setBackgroundResource(R.drawable.checked);
                        done = "0";
                    } else {
                        done = "1";
                        holder.done.setBackgroundResource(R.drawable.un_check);
                    }
                    final String finalDone = done;
                    holder.doneLayout.setClickable(true);
                    holder.doneLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((ScheduleCalendarActivity) mContext).setDone(data.getId(), finalDone);
                        }
                    });
                } else {
                    holder.done.setVisibility(View.INVISIBLE);
                    holder.doneLayout.setClickable(false);
                }


            } else if (mListData.get(position) instanceof EventListData) {
                holder.done.setVisibility(View.GONE);
                holder.doneLayout.setClickable(false);
                EventListData data = (EventListData) mListData.get(position);
                startTime = Long.parseLong(data.getActivityTime()) * 1000;
                holder.time.setText(DateUtil.getHourAndMin(startTime));
                int hour = DateUtil.getHour(startTime);
                if (hour <= 12) {
                    holder.dayTime.setBackgroundResource(R.drawable.calendar_morning_iv);
                } else if (hour >= 18) {
                    holder.dayTime.setBackgroundResource(R.drawable.calendar_evening_iv);
                } else {
                    holder.dayTime.setBackgroundResource(R.drawable.calendar_afternoon_iv);
                }
                if (!StringUtil.isEmpty(data.getEndTime())) {
                    Long endTime = Long.parseLong(data.getEndTime()) * 1000;
                    if (endTime != 0) {
                        holder.time.append("-" + DateUtil.getHourAndMin(endTime));
                    }
                }
                holder.time.append("     地点：" + data.getActivitySite());
                holder.title.setText(data.getEventTitle());
                holder.title.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                holder.time.setTextColor(mContext.getResources().getColor(R.color.schedule_bule_text1));
                holder.title.setTextColor(mContext.getResources().getColor(R.color.schedule_bule_text1));
                if ("1".equals(data.getHasRead())) {
                    holder.status.setVisibility(View.GONE);
                } else {
                    holder.status.setVisibility(View.VISIBLE);
                }
                holder.type.setVisibility(View.VISIBLE);
            }

        }
        // 判断红线显示位置
        if (mRedLineShow && startTime >= System.currentTimeMillis()) {
            mRedLineShow = false;
            holder.mRedLine.setVisibility(View.VISIBLE);
            holder.currentTime.setText(DateUtil.getHourAndMin(System.currentTimeMillis()));
        } else {
            holder.mRedLine.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        ImageView dayTime, status, type, done;
        TextView time, title, currentTime;
        LinearLayout doneLayout;
        RelativeLayout mRedLine;
    }

}
